package com.sunny.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Random;

/**
 * Created by Sunny on 2016/4/15.
 */

public class NumberUtil {
    public NumberUtil() {
    }

    public static boolean andOperate(int value1, int value2) {
        return (value1 & value2) == value2;
    }

    public static int getRandomInt(int min, int max) {
        return (int)Math.round(Math.random() * (double)(max - min) + (double)min);
    }

    public static int parseInt(String in) {
        return parseInt(in, 0);
    }

    public static int parseInt(String in, int defaultValue) {
        int re = defaultValue;
        if(!StringUtils.isEmpty(in)) {
            try {
                re = Integer.parseInt(in);
            } catch (Exception var4) {
                re = defaultValue;
            }
        }

        return re;
    }

    public static long parseLong(String in) {
        return parseLong(in, 0L);
    }

    public static long parseLong(String in, long defaultValue) {
        long re = defaultValue;
        if(!StringUtils.isEmpty(in)) {
            try {
                re = Long.parseLong(in);
            } catch (Exception var6) {
                re = defaultValue;
            }
        }

        return re;
    }

    public static double parseDouble(String in) {
        return parseDouble(in, 0.0D);
    }

    public static double parseDouble(String in, double defaultValue) {
        double re = defaultValue;
        if(!StringUtils.isEmpty(in)) {
            try {
                re = Double.parseDouble(in);
            } catch (Exception var6) {
                re = defaultValue;
            }
        }

        return re;
    }

    public static int[] parseInt(String[] in) {
        int[] arr = new int[in.length];
        int i = 0;
        String[] var6 = in;
        int var5 = in.length;

        for(int var4 = 0; var4 < var5; ++var4) {
            String s = var6[var4];
            arr[i] = parseInt(s);
            ++i;
        }

        return arr;
    }

    public static String getRandomNumberWithFixLength(int digCount) {
        Random rnd = new Random();
        StringBuilder sb = new StringBuilder(digCount);

        for(int i = 0; i < digCount; ++i) {
            sb.append((char)(48 + rnd.nextInt(10)));
        }

        return sb.toString();
    }

    public static double divide(int scale, Long dividend, Long divisor) {
        Long zero = new Long(0L);
        if(dividend != null && !zero.equals(dividend) && divisor != null && !zero.equals(divisor)) {
            BigDecimal ret = null;

            try {
                ret = (new BigDecimal(dividend.longValue())).divide(new BigDecimal(divisor.longValue()), scale, 4);
            } catch (Exception var6) {
                return 0.0D;
            }

            return ret.doubleValue();
        } else {
            return 0.0D;
        }
    }

    public static double divide(int scale, Double dividend, Long divisor) {
        Long zero = new Long(0L);
        if(dividend != null && !zero.equals(dividend) && divisor != null && !zero.equals(divisor)) {
            BigDecimal ret = null;

            try {
                ret = (new BigDecimal(dividend.doubleValue())).divide(new BigDecimal(divisor.longValue()), scale, 4);
            } catch (Exception var6) {
                return 0.0D;
            }

            return ret.doubleValue();
        } else {
            return 0.0D;
        }
    }

    public static double divide(int scale, Double dividend, Double divisor) {
        Long zero = new Long(0L);
        if(dividend != null && !zero.equals(dividend) && divisor != null && !zero.equals(divisor)) {
            BigDecimal ret = null;

            try {
                ret = (new BigDecimal(dividend.doubleValue())).divide(new BigDecimal(divisor.doubleValue()), scale, 4);
            } catch (Exception var6) {
                return 0.0D;
            }

            return ret.doubleValue();
        } else {
            return 0.0D;
        }
    }

    public static double divide(int scale, String dividend, String divisor) {
        Long zero = new Long(0L);
        if(dividend != null && !zero.equals(dividend) && divisor != null && !zero.equals(divisor)) {
            BigDecimal ret = null;

            try {
                ret = (new BigDecimal(dividend)).divide(new BigDecimal(divisor), scale, 4);
            } catch (Exception var6) {
                return 0.0D;
            }

            return ret.doubleValue();
        } else {
            return 0.0D;
        }
    }

    public static Long liConvertFeng(Long liValue) {
        if(liValue != null && liValue.longValue() != 0L) {
            BigDecimal ret = null;

            try {
                ret = (new BigDecimal(liValue.longValue())).divide(new BigDecimal(10), 0, 4);
            } catch (Exception var3) {
                return Long.valueOf(0L);
            }

            return Long.valueOf(ret.longValue());
        } else {
            return Long.valueOf(0L);
        }
    }
}
